package com.aishua.aishuawallet.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.aishua.aishuawallet.Constant;
import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.utils.CacheUtils;

import java.util.Locale;

/**
 * Created by aishua on 2018/8/8.
 */

public class LanguageActivity extends BaseActivity {

    private TextView tv_china;
    private TextView tv_english;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_language);
        initVIew();
        bussiness();
    }


    private void initVIew() {

        tv_china = findViewById(R.id.tv_china);
        tv_english = findViewById(R.id.tv_english);

    }

    private void bussiness() {

        tv_china.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CacheUtils.putlanguage(LanguageActivity.this, Constant.LAGUAGE,Constant.CHINA_LAGUAGE);
                changeAppLanguage(Locale.SIMPLIFIED_CHINESE);
            }
        });

        tv_english.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CacheUtils.putlanguage(LanguageActivity.this, Constant.LAGUAGE,Constant.US_LAGUAGE);
                changeAppLanguage(Locale.US);

            }
        });

    }


    public void changeAppLanguage(Locale locale) {

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        Configuration configuration = getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        getResources().updateConfiguration(configuration, metrics);
        //重新启动Activity
        Intent intent = new Intent(this, WelcomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

    }

}
